package com.server.modules.sys.utils;

import com.server.common.utils.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class AmountUtils {
	/**金额为分的格式 */    
    public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";    
        
    /**   
     * 将分为单位的转换为元并返回金额格式的字符串 （除100）  
     *   
     * @param amount  
     * @return  
     * @throws Exception   
     */    
    public static String changeF2Y(Long amount) {    
      
        int flag = 0;    
        String amString = amount.toString();    
        if(amString.charAt(0)=='-'){    
            flag = 1;    
            amString = amString.substring(1);    
        }    
        StringBuffer result = new StringBuffer();    
        if(amString.length()==1){    
            result.append("0.0").append(amString);    
        }else if(amString.length() == 2){    
            result.append("0.").append(amString);    
        }else{    
            String intString = amString.substring(0,amString.length()-2);    
            for(int i=1; i<=intString.length();i++){    
                if( (i-1)%3 == 0 && i !=1){    
                    result.append(",");    
                }    
                result.append(intString.substring(intString.length()-i,intString.length()-i+1));    
            }    
            result.reverse().append(".").append(amString.substring(amString.length()-2));    
        }    
        if(flag == 1){    
            return "-"+result.toString();    
        }else{    
            return result.toString();    
        }    
    }    
    
   
        
    /**  
     * 将分为单位的转换为元 （除100）  
     *   
     * @param amount  
     * @return  
     * @throws Exception   
     */    
    public static String changeF2Y(String amount) {
        if(StringUtils.isBlank(amount)){
            return "0";
        }
        return BigDecimal.valueOf(Double.valueOf(amount)).divide(new BigDecimal(100)).toString();
    }

    /**
     * 将分为单位的转换为元 （除1000000）
     *
     * @param amount
     * @return
     * @throws Exception
     */
    public static String changeF2W(String amount) {
        return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(1000000)).toString();
    }


    /**  
     * 将分为单位的转换为元 （除100）  
     *   
     * @param amount  
     * @return  
     * @throws Exception   
     */    
    public static String changeF2YWithUnit(String amount,String uinit) throws Exception{    
        if(!amount.matches(CURRENCY_FEN_REGEX)) {    
            throw new Exception("金额格式有误");    
        }    
        return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString()+uinit;    
    } 
    public static String changeF2YWithUnit(Long amount,String uinit) throws Exception{    

        return BigDecimal.valueOf(amount).divide(new BigDecimal(100)).toString()+uinit;    
    } 
    /**   
     * 将元为单位的转换为分 （乘100）  
     *   
     * @param amount  
     * @return  
     */    
    public static String changeY2F(Long amount){    
        return BigDecimal.valueOf(amount).multiply(new BigDecimal(100)).toString();    
    }
    /**
     * 将万元为单位的转换为分 （乘1000000）
     *
     * @param amount
     * @return
     */
    public static String changeW2F(Long amount){
        return BigDecimal.valueOf(amount).multiply(new BigDecimal(1000000)).toString();
    }





    /**   
     * 将元为单位的转换为分 替换小数点，支持以逗号区分的金额  
     *   
     * @param amount  
     * @return  
     */    
    public static String changeY2F(String amount){    
        String currency =  amount.replaceAll("\\$|\\￥|\\,", "");  //处理包含, ￥ 或者$的金额    
        int index = currency.indexOf(".");    
        int length = currency.length();    
        Long amLong = 0l;    
        if(index == -1){    
            amLong = Long.valueOf(currency+"00");    
        }else if(length - index >= 3){    
            amLong = Long.valueOf((currency.substring(0, index+3)).replace(".", ""));    
        }else if(length - index == 2){    
            amLong = Long.valueOf((currency.substring(0, index+2)).replace(".", "")+0);    
        }else{    
            amLong = Long.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");    
        }    
        return amLong.toString();    
    }

    /**
     * 格式化金额
     * @param s
     * @param len
     * @return
     */
    public static String formatMoney(String s, int len)
    {
        if (s == null || s.length() < 1) {
            return "";
        }
        NumberFormat formater = null;
        double num = Double.parseDouble(s);
        if (len == 0) {
            formater = new DecimalFormat("###,###");

        } else {
            StringBuffer buff = new StringBuffer();
            buff.append("###,###.");
            for (int i = 0; i < len; i++) {
                buff.append("#");
            }
            formater = new DecimalFormat(buff.toString());
        }
        String result = formater.format(num);
        if(result.indexOf(".") == -1)
        {
            result =  result + ".00";
        }
        else
        {
            result =  result;
        }
        return result;
    }
    public static String handelAmount(String amount){
        try {
            if(StringUtils.isBlank(amount)){
                return "0.00元";
            }
            Double aDouble=0.0;
            aDouble = Double.valueOf(amount);
            if(aDouble>500000){
                amount=formatMoney(AmountUtils.changeF2Y(amount),2)+"元";
                return amount;
            }else{
                //万元转为元
                BigDecimal amountbd=new BigDecimal(amount);
                BigDecimal unit=new BigDecimal("10000");
                BigDecimal yAmout = amountbd.multiply(unit);
                amount=   formatMoney(yAmout.floatValue()+"",2)+"元";
                return amount;
            }
        }catch (Exception e){
            //转换失败
            e.printStackTrace();
        }
        return amount+"万元";
    }

    public static void main(String[] args) {
        String s1 = changeY2F("500000");
        System.out.println(s1);

      /*  String memberLoanLimit="4444";
        //万转为分存
        try {
            try {
                String s = handelAmount("1230000.00");
                System.out.println(s);
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            //System.out.println(AmountUtils.changeF2YWithUnit("1322","元"));
        } catch (Exception e) {
            e.printStackTrace();
        }*/
//        System.out.println(Long.parseLong(AmountUtils.changeY2F("1000000000000000")));
//        System.out.println(Integer.parseInt(AmountUtils.changeY2F("10000000")));
//        System.out.println(Integer.MIN_VALUE);
//        long a = 0;
//        System.out.println(a);

    }



}
